import utils.JDBCUtil01;
import utils.JDBCUtil02;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        //获取表单提交的数据
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //连接数据库
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        int count=0;
        try {
            conn=JDBCUtil01.getConnection();
            String sql01="select * from t_user";
            ps=conn.prepareStatement(sql01);
            rs=ps.executeQuery();
            rs.last();
            int rowCount=rs.getRow();
            if(rowCount==2){
                response.sendRedirect(request.getContextPath()+"/UsernameOver.html");
            }else{
                try {
                    conn=JDBCUtil01.getConnection();
                    String sql="select username from t_user where username=?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1,username);
                    rs=ps.executeQuery();
                    if (rs.next()){
                        response.sendRedirect(request.getContextPath()+"/UsernameRepeat.html");
                    }else{
                        try {
                            conn= JDBCUtil02.getConnection();
                            //关闭自动提交机制
                            conn.setAutoCommit(false);
                            String sql02="insert into t_user (username,password)values(?,?)";
                            ps=conn.prepareStatement(sql02);
                            ps.setString(1,username);
                            ps.setString(2,password);
                            count=ps.executeUpdate();
                            //提交事务
                            conn.commit();
                        } catch (Exception e) {
                            if (conn != null) {
                                try {
                                    conn.rollback();
                                } catch (SQLException throwables) {
                                    throwables.printStackTrace();
                                }
                            }
                            e.printStackTrace();
                        }finally{
                            JDBCUtil02.close(conn,ps);
                        }
                        //判断注册成功则跳转至注册成功页面
                        if(count==1){
                            response.sendRedirect(request.getContextPath()+"/RegisterSuccess.html");
                        }
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    JDBCUtil01.close(conn,ps,rs);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            JDBCUtil01.close(conn,ps,rs);
        }
    }
}
